package org.example.area;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang.StringUtils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class AreaList {
    private static volatile AreaList instance;
    private Map<String, String> areaData;

    private AreaList() {
        loadAreaData();
    }

    public static AreaList getInstance() {
        if (instance == null) {
            synchronized (AreaList.class) {
                if (instance == null) {
                    instance = new AreaList();
                }
            }
        }
        return instance;
    }

    private void loadAreaData() {
        try {
            InputStream inputStream = getClass().getClassLoader()
                    .getResourceAsStream("data/xzqh.json");
            ObjectMapper mapper = new ObjectMapper();

            List<Xzqh> xzqhs = mapper.readValue(inputStream, new TypeReference<List<Xzqh>>() {});
            areaData=new HashMap<>();
            for(Xzqh xzqh:xzqhs){
                areaData.put(xzqh.getQhdm(), xzqh.getQhmc());
            }

        } catch (Exception e) {
            throw new RuntimeException("Failed to load area data", e);
        }
    }

    public Map<String, String> getAreaData() {
        return areaData;
    }

    /**
     * 根据区划代码查询名称
     * @param code
     * @return
     */
    public static String getAreaName(String code) {
        if(instance==null){
            instance=AreaList.getInstance();
        }
        return instance.areaData.get(code);
    }
    public static String getFullName(String code) {
        String name="";
        if(StringUtils.isBlank( code)){
            return name;
        }
        if(code.length()>=2){
            name=getAreaName(code.substring(0,2));
        }
        if(code.length()>=4){
            name+=getAreaName(code.substring(0,4));
        }
        if(code.length()>=6){
            name+=getAreaName(code.substring(0,6));
        }
        if(code.length()>=9){
            name+=getAreaName(code.substring(0,9));
        }
        if(code.length()==12){
            name+=getAreaName(code.substring(0,12));
        }
        return name;
    }
    public static AreaDto getFullNameEntity(String code) {
        AreaDto areaDto = new AreaDto();

        if(StringUtils.isBlank( code)){
            return areaDto;
        }
        if(code.length()>=2){
            areaDto.setProvinceName(getAreaName(code.substring(0,2)));
        }
        if(code.length()>=4){
            areaDto.setCityName(getAreaName(code.substring(0,4)));
        }
        if(code.length()>=6){
            areaDto.setCountyName(getAreaName(code.substring(0,6)));
        }
        if(code.length()>=9){
            areaDto.setTownName(getAreaName(code.substring(0,9)));
        }
        if(code.length()==12){
            areaDto.setVillageName(getAreaName(code.substring(0,12)));
        }
        return areaDto;
    }
}
